/**
 * 
 */
package com.alphasystem.commerce.common.model.locale;

import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.InheritanceType.JOINED;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

/**
 * @author Syed Farhan Ali
 * 
 */
@Entity
@Table(name = "COUNTRY")
@Inheritance(strategy = JOINED)
public class Country extends LocaleEntity {

	private static final long serialVersionUID = -5962558918594750997L;

	protected String countryCode;

	protected Set<Currency> currencies;

	protected Set<Language> languages;

	public Country() {
		this(TEMP_ID, TEMP_ID);
	}

	public Country(String countryCode, String name) {
		super(countryCode);
		this.countryCode = countryCode;
		this.name = name;
		languages = new HashSet<Language>();
		currencies = new HashSet<Currency>();
	}

	public boolean addCurrency(Currency o) {
		boolean add = false;
		if (!currencies.contains(o)) {
			add = currencies.add(o);
		}
		return add;
	}

	public boolean addLanguage(Language o) {
		boolean add = false;
		if (!languages.contains(o)) {
			add = languages.add(o);
		}
		return add;
	}

	@Column(name = "COUNTRY_CODE", length = 2, unique = true, nullable = false)
	public String getCountryCode() {
		return countryCode;
	}

	@ManyToMany(fetch = EAGER, cascade = { PERSIST, MERGE })
	@JoinTable(name = "COUNTRY_CURRENCY_RLTN", joinColumns = { @JoinColumn(name = "country_id") }, inverseJoinColumns = { @JoinColumn(name = "currency_id") })
	public Set<Currency> getCurrencies() {
		return currencies;
	}

	@ManyToMany(fetch = EAGER, cascade = { PERSIST, MERGE })
	@JoinTable(name = "COUNTRY_LANGUAGE_RLTN", joinColumns = { @JoinColumn(name = "country_id") }, inverseJoinColumns = { @JoinColumn(name = "language_id") })
	public Set<Language> getLanguages() {
		return languages;
	}

	public boolean removeCurrency(Currency o) {
		return currencies.remove(o);
	}

	public boolean removeLanguage(Language o) {
		return languages.remove(o);
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public void setCurrencies(Set<Currency> currencies) {
		this.currencies = currencies;
	}

	public void setLanguages(Set<Language> languages) {
		this.languages = languages;
	}

	@Override
	public String toString() {
		return "Country: " + countryCode + ":" + name;
	}
}
